var params = {}, tableIns;
layui.use(['table','layer','jquery','element','sidebar','form'], function(){
    var $ = jQuery = layui.$;
    var table = layui.table;
    var layer = layui.layer;
    var element = layui.element;
    var sidebar = layui.sidebar;
    var form = layui.form;
    tableIns = table.render({
        elem: '#modulestable',
        height:'full-113',
        size:'sm',
        url: ctx+'/sys_admin/modules/getList',
        page: true,
        even: true,
        cols: [[
            {type: 'numbers',fixed: 'left'},
            {field: 'id', title: 'ID',fixed: 'left', align:'center',width:180, hide:true},
            {field: 'name', title: '模块名称',fixed: 'left', align:'center',width:200},
            {field: 'tablename', title: '库表编号', align:'center',width:180, hide:true},
            {field: 'wholetablename', title: '库表名称', align:'center',width:180, hide:true},
            {field: 'url', title: '访问地址', align:'center'},
            {field: 'createdate', title: '创建时间', align:'center', width:200},
            {field: 'username', title: '创建人', align:'center', width:150},
            {field: 'is_interface', title:'是否启用接口数据', templet: '#isInterface', unresize: true, width:150},
            {field: 'status', title:'状态', templet: '#enabled', unresize: true, width:150},
            {title: '操作', width: 650, fixed: 'right', align:'center',toolbar: '#toolbar'}
        ]],
        done:function(res, curr){
            params['curr'] = curr;
        }
    });
    table.on('tool(modules)', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){
            layer.confirm('确定删除行么？', function(index){
                $.post(ctx+'/sys_admin/modules/delete', {"ids":data.id}, function(result) {
                    if (result.success) {
                        obj.del();
                        layer.msg("删除成功!")
                        layer.close(index);
                    } else {
                        layer.msg(result.msg);
                    }
                }, 'json');
            });
        } else if(obj.event === 'editForm'){
            layer.open({
                title:"表单管理",
                type: 2,
                area: ['90%', '90%'],
                fixed: false, //不固定
                maxmin: true,
                content: ctx+'/sys_admin/modules/columns?moduleid='+data.id+'&tablename='+data.wholetablename
            });
        } else if (obj.event === 'cal'){
            layer.open({
                title:"计算公式",
                type: 2,
                area: ['80%', '80%'],
                fixed: false, //不固定
                maxmin: true,
                content: ctx+'/sys_admin/calculate?moduleid='+data.id
            });
        }  else if(obj.event === 'edit'){
            layer.open({
                title:"编辑",
                type: 2,
                area: ['80%', '80%'],
                fixed: false, //不固定
                maxmin: true,
                content: ctx+'/sys_admin/modules/edit?id='+data.id,
                btn: ['提交', '重置','取消'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.table = tableIns;
                    iframeWin.contentWindow.params = params;
                    layero.find('iframe').contents().find('#submit-btn').click();
                },
                btn2: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.table = tableIns;
                    layero.find('iframe').contents().find('#reset-btn').click();
                    return false;
                },
                btn3: function(index, layero){
                    var index = layer.getFrameIndex(window.name);
                    layer.close(index);
                }
            });
        }  else if(obj.event === 'search'){
            parent.layer.open({
                title:"自定义查询项",
                type: 2,
                area: ['706px', '80%'],
                fixed: false, //不固定
                maxmin: true,
                content: ctx+'/sys_admin/modules/searchForm?tablename='+tablename+'&moduleid='+data.id,
                btn: ['保存', '关闭'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.table = location;
                    iframeWin.contentWindow.params = params;
                    layero.find('iframe').contents().find('#submit-btn').click();
                },
                btn3: function(index, layero){
                    var index = layer.getFrameIndex(window.name);
                    layer.close(index);
                }
            });
        }else if(obj.event === 'competence'){
            parent.layer.open({
                title:"数据权限",
                type: 2,
                area: ['60%', '80%'],
                fixed: false, //不固定
                maxmin: true,
                content: ctx+'/sys_admin/modules/competence?id='+data.id,
                btn: ['关闭'],
                /*
                btn: ['保存', '关闭'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对象，执行iframe页的方法：
                    iframeWin.contentWindow.table = tableIns;
                    layero.find('iframe').contents().find('#submit-btn').click();
                },*/
                btn2: function(index, layero){
                    var index = layer.getFrameIndex(window.name);
                    layer.close(index); //再执行关闭
                }
            });
        }else if(obj.event === 'menuCompetence'){
            parent.layer.open({
                title:"菜单权限",
                type: 2,
                area: ['30%', '80%'],
                fixed: false, //不固定
                maxmin: true,
                content: '/sys_admin/modules/menuCompetence?id='+data.id,
                btn: ['保存', '关闭'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对象，执行iframe页的方法：
                    iframeWin.contentWindow.table = tableIns;
                    layero.find('iframe').contents().find('#submit-btn').click();
                },
                btn2: function(index, layero){
                    var index = layer.getFrameIndex(window.name);
                    layer.close(index); //再执行关闭
                }
            });
        }else if(obj.event === 'order'){
            parent.layer.open({
                title:"自定义排序项",
                type: 2,
                area: ['60%', '80%'],
                fixed: false, //不固定
                maxmin: true,
                content: ctx+'/sys_admin/modules/order?id='+data.id,
                btn: ['关闭'],
                /*
                btn: ['保存', '关闭'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];//得到iframe页的窗口对象，执行iframe页的方法：
                    iframeWin.contentWindow.table = tableIns;
                    layero.find('iframe').contents().find('#submit-btn').click();
                },*/
                btn2: function(index, layero){
                    var index = layer.getFrameIndex(window.name);
                    layer.close(index); //再执行关闭
                }
            });
        }else if(obj.event === 'interfaceSeting'){
            layer.open({
                title:"接口配置",
                type: 2,
                area: ['70%', '95%'],
                fixed: false, //不固定
                maxmin: true,
                content: ctx+'/sys_admin/modules/interfaceSeting?moduleid='+data.id,
                btn: ['确定'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.table = location;
                    iframeWin.contentWindow.params = params;
                    layero.find('iframe').contents().find('#submit-btn').click();
                },
            });
        }
    });

    form.on('select(status)', function(data){
        $('#submitid').click();
    });

    form.on('switch(status)', function(data){
        var selectIfKey = data.othis;
        var parentTr = selectIfKey.parents("tr");
        var parentTrIndex = parentTr.attr("data-index");
        var id = parentTr.find(('td:eq(1)')).text().trim();
        var state = 2;
        if(data.elem.checked){state = 1;}
        $.post(ctx+'/sys_admin/modules/setStatus', {ids: id, status: state}, function() {}, 'json');
    });

    form.on('switch(isInterface)', function(data){
        var selectIfKey = data.othis;
        var parentTr = selectIfKey.parents("tr");
        var parentTrIndex = parentTr.attr("data-index");
        var id = parentTr.find(('td:eq(1)')).text().trim();
        var state = 1;
        if(data.elem.checked){state = 2;}
        $.post(ctx+'/sys_admin/modules/setIsInterface', {id: id, status: state}, function() {}, 'json');
    });

    $("#add").on('click',function(){
        layer.open({
            type: 2,
            area: ['90%', '90%'],
            fixed: false, //不固定
            maxmin: true,
            content: ctx+'/sys_admin/modules/form',
            btn: ['提交', '重置','取消'],
            yes: function(index, layero){
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.table = tableIns;
                iframeWin.contentWindow.params = params;
                layero.find('iframe').contents().find('#submit-btn').click();
            },
            btn2: function(index, layero){
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.table = tableIns;
                layero.find('iframe').contents().find('#reset-btn').click();
                return false;
            },
            btn3: function(index, layero){
                var index = layer.getFrameIndex(window.name);
                layer.close(index);
            }
        });
    });

    form.on('submit(serach_form)', function (data) {
        table.reload('modulestable', {
            method : 'post',
            where: {
                name : data.field.name,
                status : data.field.status
            },
            page: {
                curr : 1 //重新从第 1 页开始
            }
        });
        params['name'] = data.field.name;
        params['status'] = data.field.status;
        return false;
    });

    $("#export").on('click',function(){
        window.open(ctx+'/modules/exportExcel');
    })

});